home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / flight-of-the-museum.swf / scripts / com / google / analytics / debug / Layout.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  9.2 KB  |  318 lines

  1. package com.google.analytics.debug
  2. {
  3.    import com.google.analytics.GATracker;
  4.    import com.google.analytics.core.GIFRequest;
  5.    import flash.display.DisplayObject;
  6.    import flash.events.Event;
  7.    import flash.events.KeyboardEvent;
  8.    import flash.net.URLRequest;
  9.    
  10.    public class Layout implements ILayout
  11.    {
  12.        
  13.       
  14.       private var _display:DisplayObject;
  15.       
  16.       private var _infoQueue:Array;
  17.       
  18.       private var _maxCharPerLine:int = 85;
  19.       
  20.       private var _hasInfo:Boolean;
  21.       
  22.       private var _warningQueue:Array;
  23.       
  24.       private var _hasDebug:Boolean;
  25.       
  26.       private var _hasWarning:Boolean;
  27.       
  28.       private var _mainPanel:Panel;
  29.       
  30.       private var _GRAlertQueue:Array;
  31.       
  32.       private var _debug:DebugConfiguration;
  33.       
  34.       public var visualDebug:Debug;
  35.       
  36.       private var _hasGRAlert:Boolean;
  37.       
  38.       public function Layout(debug:DebugConfiguration, display:DisplayObject)
  39.       {
  40.          super();
  41.          _display = display;
  42.          _debug = debug;
  43.          _hasWarning = false;
  44.          _hasInfo = false;
  45.          _hasDebug = false;
  46.          _hasGRAlert = false;
  47.          _warningQueue = [];
  48.          _infoQueue = [];
  49.          _GRAlertQueue = [];
  50.       }
  51.       
  52.       private function onKey(event:KeyboardEvent = null) : void
  53.       {
  54.          switch(event.keyCode)
  55.          {
  56.             case _debug.showHideKey:
  57.                _mainPanel.visible = !_mainPanel.visible;
  58.                break;
  59.             case _debug.destroyKey:
  60.                destroy();
  61.          }
  62.       }
  63.       
  64.       public function createWarning(message:String) : void
  65.       {
  66.          if(_hasWarning || !isAvailable())
  67.          {
  68.             _warningQueue.push(message);
  69.             return;
  70.          }
  71.          message = _filterMaxChars(message);
  72.          _hasWarning = true;
  73.          var w:Warning = new Warning(message,_debug.warningTimeout);
  74.          addToPanel("analytics",w);
  75.          w.addEventListener(Event.REMOVED_FROM_STAGE,_clearWarning,false,0,true);
  76.          if(_hasDebug)
  77.          {
  78.             visualDebug.writeBold(message);
  79.          }
  80.       }
  81.       
  82.       public function bringToFront(visual:DisplayObject) : void
  83.       {
  84.          _display.stage.setChildIndex(visual,_display.stage.numChildren - 1);
  85.       }
  86.       
  87.       public function createFailureAlert(message:String) : void
  88.       {
  89.          var actionClose:AlertAction = null;
  90.          if(_debug.verbose)
  91.          {
  92.             message = _filterMaxChars(message);
  93.             actionClose = new AlertAction("Close","close","close");
  94.          }
  95.          else
  96.          {
  97.             actionClose = new AlertAction("X","close","close");
  98.          }
  99.          var fa:Alert = new FailureAlert(_debug,message,[actionClose]);
  100.          addToPanel("analytics",fa);
  101.          if(_hasDebug)
  102.          {
  103.             if(_debug.verbose)
  104.             {
  105.                message = message.split("\n").join("");
  106.                message = _filterMaxChars(message,66);
  107.             }
  108.             visualDebug.writeBold(message);
  109.          }
  110.       }
  111.       
  112.       public function init() : void
  113.       {
  114.          var spaces:int = 10;
  115.          var W:uint = uint(_display.stage.stageWidth - spaces * 2);
  116.          var H:uint = uint(_display.stage.stageHeight - spaces * 2);
  117.          var mp:Panel = new Panel("analytics",W,H);
  118.          mp.alignement = Align.top;
  119.          mp.stickToEdge = false;
  120.          mp.title = "Google Analytics v" + GATracker.version;
  121.          _mainPanel = mp;
  122.          addToStage(mp);
  123.          bringToFront(mp);
  124.          if(_debug.minimizedOnStart)
  125.          {
  126.             _mainPanel.onToggle();
  127.          }
  128.          createVisualDebug();
  129.          _display.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKey,false,0,true);
  130.       }
  131.       
  132.       public function addToPanel(name:String, visual:DisplayObject) : void
  133.       {
  134.          var panel:Panel = null;
  135.          var d:DisplayObject = _display.stage.getChildByName(name);
  136.          if(d)
  137.          {
  138.             panel = d as Panel;
  139.             panel.addData(visual);
  140.          }
  141.          else
  142.          {
  143.             trace("panel \"" + name + "\" not found");
  144.          }
  145.       }
  146.       
  147.       private function _clearInfo(event:Event) : void
  148.       {
  149.          _hasInfo = false;
  150.          if(_infoQueue.length > 0)
  151.          {
  152.             createInfo(_infoQueue.shift());
  153.          }
  154.       }
  155.       
  156.       private function _filterMaxChars(message:String, maxCharPerLine:int = 0) : String
  157.       {
  158.          var line:String = null;
  159.          var CRLF:String = "\n";
  160.          var output:Array = [];
  161.          var lines:Array = message.split(CRLF);
  162.          if(maxCharPerLine == 0)
  163.          {
  164.             maxCharPerLine = _maxCharPerLine;
  165.          }
  166.          for(var i:int = 0; i < lines.length; i++)
  167.          {
  168.             line = String(lines[i]);
  169.             while(line.length > maxCharPerLine)
  170.             {
  171.                output.push(line.substr(0,maxCharPerLine));
  172.                line = line.substring(maxCharPerLine);
  173.             }
  174.             output.push(line);
  175.          }
  176.          return output.join(CRLF);
  177.       }
  178.       
  179.       private function _clearGRAlert(event:Event) : void
  180.       {
  181.          _hasGRAlert = false;
  182.          if(_GRAlertQueue.length > 0)
  183.          {
  184.             createGIFRequestAlert.apply(this,_GRAlertQueue.shift());
  185.          }
  186.       }
  187.       
  188.       public function createSuccessAlert(message:String) : void
  189.       {
  190.          var actionClose:AlertAction = null;
  191.          if(_debug.verbose)
  192.          {
  193.             message = _filterMaxChars(message);
  194.             actionClose = new AlertAction("Close","close","close");
  195.          }
  196.          else
  197.          {
  198.             actionClose = new AlertAction("X","close","close");
  199.          }
  200.          var sa:Alert = new SuccessAlert(_debug,message,[actionClose]);
  201.          addToPanel("analytics",sa);
  202.          if(_hasDebug)
  203.          {
  204.             if(_debug.verbose)
  205.             {
  206.                message = message.split("\n").join("");
  207.                message = _filterMaxChars(message,66);
  208.             }
  209.             visualDebug.writeBold(message);
  210.          }
  211.       }
  212.       
  213.       public function isAvailable() : Boolean
  214.       {
  215.          return _display.stage != null;
  216.       }
  217.       
  218.       public function createAlert(message:String) : void
  219.       {
  220.          message = _filterMaxChars(message);
  221.          var a:Alert = new Alert(message,[new AlertAction("Close","close","close")]);
  222.          addToPanel("analytics",a);
  223.          if(_hasDebug)
  224.          {
  225.             visualDebug.writeBold(message);
  226.          }
  227.       }
  228.       
  229.       public function createInfo(message:String) : void
  230.       {
  231.          if(_hasInfo || !isAvailable())
  232.          {
  233.             _infoQueue.push(message);
  234.             return;
  235.          }
  236.          message = _filterMaxChars(message);
  237.          _hasInfo = true;
  238.          var i:Info = new Info(message,_debug.infoTimeout);
  239.          addToPanel("analytics",i);
  240.          i.addEventListener(Event.REMOVED_FROM_STAGE,_clearInfo,false,0,true);
  241.          if(_hasDebug)
  242.          {
  243.             visualDebug.write(message);
  244.          }
  245.       }
  246.       
  247.       public function createGIFRequestAlert(message:String, request:URLRequest, ref:GIFRequest) : void
  248.       {
  249.          var gra:GIFRequestAlert;
  250.          var f:Function;
  251.          if(_hasGRAlert)
  252.          {
  253.             _GRAlertQueue.push([message,request,ref]);
  254.             return;
  255.          }
  256.          _hasGRAlert = true;
  257.          f = function():void
  258.          {
  259.             ref.sendRequest(request);
  260.          };
  261.          message = _filterMaxChars(message);
  262.          gra = new GIFRequestAlert(message,[new AlertAction("OK","ok",f),new AlertAction("Cancel","cancel","close")]);
  263.          addToPanel("analytics",gra);
  264.          gra.addEventListener(Event.REMOVED_FROM_STAGE,_clearGRAlert,false,0,true);
  265.          if(_hasDebug)
  266.          {
  267.             if(_debug.verbose)
  268.             {
  269.                message = message.split("\n").join("");
  270.                message = _filterMaxChars(message,66);
  271.             }
  272.             visualDebug.write(message);
  273.          }
  274.       }
  275.       
  276.       public function createVisualDebug() : void
  277.       {
  278.          if(!visualDebug)
  279.          {
  280.             visualDebug = new Debug();
  281.             visualDebug.alignement = Align.bottom;
  282.             visualDebug.stickToEdge = true;
  283.             addToPanel("analytics",visualDebug);
  284.             _hasDebug = true;
  285.          }
  286.       }
  287.       
  288.       public function addToStage(visual:DisplayObject) : void
  289.       {
  290.          _display.stage.addChild(visual);
  291.       }
  292.       
  293.       private function _clearWarning(event:Event) : void
  294.       {
  295.          _hasWarning = false;
  296.          if(_warningQueue.length > 0)
  297.          {
  298.             createWarning(_warningQueue.shift());
  299.          }
  300.       }
  301.       
  302.       public function createPanel(name:String, width:uint, height:uint) : void
  303.       {
  304.          var p:Panel = new Panel(name,width,height);
  305.          p.alignement = Align.center;
  306.          p.stickToEdge = false;
  307.          addToStage(p);
  308.          bringToFront(p);
  309.       }
  310.       
  311.       public function destroy() : void
  312.       {
  313.          _mainPanel.close();
  314.          _debug.layout = null;
  315.       }
  316.    }
  317. }
  318.